<template>
  <div class="users">
    <div class="systemTitle">光谷智慧交通系统管理平台</div>
    <div class="goSystem">
      <div class="typeUser">
        <span
          :class="{ current: selectedType === 'common' }"
          @click="changeType('common')"
          >普通用户</span
        >
        <span
          :class="{ current: selectedType === 'traffic' }"
          @click="changeType('traffic')"
          >交管部门</span
        >
        <span
          :class="{ current: selectedType === 'admin' }"
          @click="changeType('admin')"
          >管理员</span
        >
      </div>
      <input
        type="text"
        class="username"
        placeholder="请输入用户名"
        v-model="username"
      />
      <div class="errorMsg" v-show="loginError">
        用户名或密码错误!请重新输入
      </div>
      <input
        type="password"
        class="password"
        placeholder="请输入密码"
        v-model="password"
      />
      <div class="errorMsg" v-show="loginError">
        用户名或密码错误!请重新输入
      </div>
      <div class="handleLine">
        <div class="remember">
          <input type="checkbox" v-model="remember" @click="handleRemember" />
          <span>记住密码</span>
        </div>
        <a class="forget" @click="forgetFn">忘记密码?</a>
      </div>
      <button class="login" @click="loginFn">登录</button>
      <button
        class="register"
        :disabled="selectedType === 'common' ? false : true"
        @click="registerFn"
      >
        新用户注册
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { getLoginHttp, getUsersHttp } from '@/api/request/index.js'
import { useRouter } from 'vue-router'

/* 路由 */
const router = useRouter()
/* 定义账号、密码变量 */
const username = ref('')
const password = ref('')
/* 当前选择的用户类型 */
const selectedType = ref('common')
/* 登录错误提示信息 显示/隐藏 */
const loginError = ref(false)
/* 登录成功的用户信息 */
const loginUserInfo = ref({})
/* 记住密码 */
const remember = ref(false)

let getUsername = localStorage.getItem('username')
let getPassword = localStorage.getItem('password')
let getRemember = localStorage.getItem('remember')
if (getUsername && getRemember) {
  username.value = getUsername
  password.value = getPassword
  remember.value = getRemember
} else {
  username.value = ''
  password.value = ''
  remember.value = false
}
/* 处理 记住密码 */
const handleRemember = () => {
  remember.value = !remember.value
  // console.log(remember.value) // 打印 记住密码状态
  // 存储 记住密码状态
  if (remember.value && username.value && password.value) {
    localStorage.setItem('username', username.value)
    localStorage.setItem('password', password.value)
    localStorage.setItem('remember', remember.value)
  } else {
    localStorage.removeItem('username')
    localStorage.removeItem('password')
    localStorage.removeItem('remember')
  }
}

/* 切换用户类型 */
let userType = localStorage.getItem('userType')
if (userType) {
  selectedType.value = userType
} else {
  selectedType.value = 'common'
}

/* 切换用户类型 */
const changeType = (type) => {
  /* 存储用户类型 */
  localStorage.setItem('userType', type)
  selectedType.value = type
  remember.value = false
  localStorage.removeItem('remember')
  console.log('选择的用户类型', selectedType.value)
}

/* 登录 不同用户类型的账号 */
const loginFn = async () => {
  const res = await getLoginHttp({
    username: username.value,
    password: password.value,
    type: selectedType.value,
  })
  console.log(res) // 打印 登录接口返回数据
  const loginData = JSON.parse(res.config.data)
  console.log('登录', loginData.type) // 打印 登录的用户类型

  let alertMsg = res.data.msg // 登录时后台的提示信息
  if (res.data.code === 200) {
    loginError.value = false // 登录成功，隐藏 错误提示信息
    alert(alertMsg) // 打印 登录成功
    loginUserInfo.value = loginData // 登录成功，获取 该用户信息
    console.log(loginUserInfo.value) // 打印 该用户信息
    /* 登录成功，跳转到 登录成功后的页面 */
    if (selectedType.value === 'common') {
      router.push({
        path: '/login/common',
        query: {
          type: loginUserInfo.value.type,
          username: loginUserInfo.value.username,
        },
      })
    } else if (selectedType.value === 'traffic') {
      router.push({
        path: '/login/traffic',
        query: {
          type: loginUserInfo.value.type,
          username: loginUserInfo.value.username,
        },
      })
    } else if (selectedType.value === 'admin') {
      router.push({
        path: '/login/admin',
        query: {
          type: loginUserInfo.value.type,
          username: loginUserInfo.value.username,
        },
      })
    }
  } else if (res.data.code === 400) {
    console.log(alertMsg) // 打印 用户名或密码错误
    loginError.value = true // 登录失败，显示 错误提示信息
    password.value = ''
  }
}

/* 注册 不同用户类型的账号 */
const registerFn = () => {
  router.push({
    path: '/register',
    query: {
      type: selectedType.value,
    },
  })
}

/* 忘记密码 */
const forgetFn = () => {
  router.push({
    path: '/forget',
    query: {
      type: selectedType.value,
    },
  })
}

/* 监听账号或密码变化 更改记住密码状态 */
watch(
  [() => username.value, () => password.value],
  ([newUsername, newPassword]) => {
    console.log('记住密码状态变化', { newUsername, newPassword }) // 打印新的用户名和密码状态
    // 根据新的用户名或密码状态更改记住密码状态
    if (newUsername || newPassword) {
      remember.value = false
      localStorage.removeItem('remember')
    }
  }
)

onMounted(async () => {
  /* 测试数据 */
  const res = await getUsersHttp({
    type: 'admin',
  })
  const resData = res.data.result
  console.log('测试接口', resData) // 打印 后台返回的用户信息
})
</script>

<style scoped>
.users {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-image: url('@/images/user.gif');
  background-size: cover;
}
.systemTitle {
  font-size: 42px;
  color: #fff;
  text-align: center;
  padding-bottom: 50px;
  margin-bottom: 40px;
}
.goSystem {
  display: flex;
  flex-direction: column;
  width: 380px;
  /* height: 300px; */
  padding: 20px;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
.typeUser,
.username,
.password,
.login,
.register {
  width: 100%;
  height: 40px;
  border-radius: 5px;
  padding: 5px 10px;
}
.typeUser {
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
  color: #333;
}
.typeUser span:hover {
  cursor: pointer;
  color: #1061a4;
}
.typeUser span.current {
  color: #1061a4;
  border-bottom: 2px solid #1061a4;
}
.errorMsg {
  font-size: 12px;
  color: red;
}
.username,
.password {
  margin-top: 10px;
  border: 1px solid #ccc;
  outline: none;
}
.handleLine {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: blue;
  margin-top: 10px;
}
.handleLine .remember {
  display: flex;
  align-items: center;
}
.handleLine .remember input {
  margin-right: 5px;
}
.handleLine a {
  float: right;
  color: blue;
  text-decoration: none;
  cursor: pointer;
}
.login {
  background-color: #0a3767;
  color: #fff;
  border: none;
  margin-top: 10px;
  cursor: pointer;
}
.login:hover {
  background-color: #1061a4;
}
.register {
  margin-bottom: 0;
  background-color: #fff;
  border: none;
  cursor: pointer;
}
.register:hover {
  background-color: #efeeee;
}
</style>
